/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the FxStruts MailReader Application.
 *
 * The Initial Developer of the Original Code is
 * Anirudh Sasikumar (http://anirudhs.chaosnet.org/).
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
*/

package com.adobe.mailreader.view
{
    import com.adobe.mailreader.events.NotifyViewEvent;
    import com.adobe.mailreader.model.MailModelLocator;
    import com.adobe.mailreader.view.components.LoginForm;
    
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    public class LoginFormMediator extends ViewMediator
    {
        public var view:LoginForm;
        
        private var timer:Timer = new Timer(3000, 1);
        
        public function LoginFormMediator()
        {
            super();
            
            notificationInterests = [MailModelLocator.VIEW_LOGGED_IN_STATE,
                                     MailModelLocator.VIEW_LOGGED_OUT_STATE,
                                     MailModelLocator.VIEW_LOGIN_FAILED,
                                     MailModelLocator.VIEW_REGISTER_CANCEL,
                                     MailModelLocator.VIEW_REGISTER_SUCCESS
                                     ];
            
        }
        
        private function clearRegisterForm():void
        {
            view.registerForm.username.text = "";
            view.registerForm.password.text = "";
            view.registerForm.password0.text = "";
            view.registerForm.fromAddress.text = "";
            view.registerForm.replyAddress.text = "";
            view.registerForm.fullname.text = "";
            view.registerForm.mediator.clearListeners();
        }
        
        protected override function handleNotification(event:NotifyViewEvent):void
        {
            if ( view == null )
                throw new Error("view cannot be null, please set view property to mxml component");
            
            switch ( event.type )
            {
                case MailModelLocator.VIEW_LOGGED_IN_STATE:
                    
                    trace("logged in");
                    break;
                    
                case MailModelLocator.VIEW_LOGGED_OUT_STATE:
                    trace("logged out");
                    view.password.text = "";
                    view.loginButton.enabled = true;
                    break;
                    
                case MailModelLocator.VIEW_REGISTER_CANCEL:
                    view.currentState = '';
                    clearRegisterForm();
                    model.notifyView(MailModelLocator.VIEW_LOGGED_OUT_STATE); 
                    break;
                    
                case MailModelLocator.VIEW_REGISTER_SUCCESS:
                    view.currentState = '';
                    clearRegisterForm();
                    model.notifyView(MailModelLocator.VIEW_LOGGED_IN_STATE); 
                    break;
                    
                case MailModelLocator.VIEW_LOGIN_FAILED:
                    trace("auth fail");
                    if ( event.data )
                    {
                        view.errText.text = event.data as String;
                    }
                    else
                    {
                        /* TODO: This has to be localized? */
                        view.errText.text = "Invalid username or password. Try again.";
                    }

                    view.loginButton.enabled = false;
                    
                    view.errText.includeInLayout = true;
                    view.errText.visible = true;
                    
                    /* Hacky timer to make our error messages
                     * disappear */
                    if ( timer.running )
                        timer.removeEventListener(TimerEvent.TIMER_COMPLETE, hideErrorText);

                    timer.addEventListener(TimerEvent.TIMER_COMPLETE, hideErrorText);
                    timer.stop();
                    timer.start();
                    
                    view.faultEffect.stop();
                    view.faultEffect.play();
                    break;
            }
        }
        
	private function hideErrorText(event:TimerEvent):void
	{
            view.errText.visible = false;
            view.loginButton.enabled = true;
            view.errText.includeInLayout = false;
	}
        
    }
    
    
}